package ustc.browser;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class BrowserActivity 
	extends Activity 
	implements OnItemClickListener,OnClickListener{
	
	//for Log info only
	private static final String TAG = "BrowserActivity";
	private	EditText			mAddressText = null;
	private ImageButton			mGoButton = null;
	private	LinearLayout		mMainLayout = null;
	private FrameLayout 		mContentView = null;
	//Controller of the open web views, implemented as 'singleton'
	private TabControl 			mTabControl = null;
	
	//View used to overview the open web views
	private ListView		mTabOverview = null;
	//Adapter that handles data for mTabOverview
	private TabAdapter 		mAdapter = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Ask the Activity to activate the progress bar on title bar
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        //Ask the Activity to display an icon on the top left
        //of the title bar.
        getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
        LayoutInflater inflater = 
        	(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //inflate the xml layout file into a layout object
        mMainLayout = (LinearLayout)inflater.inflate(R.layout.main, null);
        setContentView(mMainLayout);
        
        mAddressText = (EditText)findViewById(R.id.address_text);
        mGoButton = (ImageButton)findViewById(R.id.go_button);
        mContentView = (FrameLayout)findViewById(R.id.content_view);
        if(null == mTabControl)
        	mTabControl = TabControl.getTabControl(this);
        
        mTabOverview = new ListView(this);
		mAdapter = new TabAdapter(this, mTabControl);
		mTabOverview.setAdapter(mAdapter);
		mTabOverview.setOnItemClickListener(this);
		mGoButton.setOnClickListener(this);
		mAddressText.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EditText txt = (EditText)v;
				txt.selectAll();
			}
		});
        
        //Set the progress bar in indeterminate mode and visible
        setProgressBarIndeterminate(true);
        setProgressBarVisibility(true);
        //Create the default web view for the browser
        createNewWindow();
    }

    //Called to set up the option menu for the browser
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		return	true;
	}

	//Handle option menu item selection
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.back:
			return true;
		case R.id.forward:
			return true;
		case R.id.new_win:
			createNewWindow();
			return true;
		case R.id.windows:
			//Change BrowserActivity's original view
			//to display a series of opened windows
			setContentView(mTabOverview);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void createNewWindow(){
        Tab t = mTabControl.createNewTab();
        if(null != t){
        	mContentView.removeAllViews();
        	mContentView.addView(t.getWebView(),
        		new ViewGroup.LayoutParams(
        			LayoutParams.FILL_PARENT,
        			LayoutParams.FILL_PARENT));
        	//FIXME: when loading not finished, the mAdapter cannot see the website title
        	//FIXME: homepage will be targeted from the preference settings
        	String url = "http://wap.sina.com.cn";
        	t.loadUrl(url);
        	setContentView(mMainLayout);
        }else{
        	Toast.makeText(this, "Error: Too many windows!",
					Toast.LENGTH_SHORT).show();
        }
	}
	//When items in mTabOverview has been clicked
	//ask BrowserActivity to display the webview.
	@Override
	public void onItemClick(AdapterView parent, View v,
			int position, long id) {
		mContentView.removeAllViews();
		WebView wv = mTabControl.getTabOfIndex(position).getWebView();
		//Remove the parent of web view
		wv.removeView((View)wv.getParent());
		mContentView.addView(wv);
		setContentView(mMainLayout);
	}
	//When Receiving new web content(title, url etc.)
	//Tell the adapter to redraw itself
	public void onWebDataReceived(){
		mAdapter.notifyDataSetChanged();
	}
	//Set the url in mAddressText
	public void setUrlInAddressText(String url){
		mAddressText.setText(url);
	}

	@Override
	public void onClick(View v) {
	//Handle mGoButton clicking
		String url = mAddressText.getText().toString();
		if(url != ""){
			Tab t = mTabControl.getCurrentTab();
			t.loadUrl(url);
		}else{
			Toast.makeText(this, "Error: Address cannot be empty",
					Toast.LENGTH_SHORT).show();
		}
	}
}